package org.fage.semples.service;

import java.util.ArrayList;
import java.util.List;

import org.fage.semples.dao.ExamSubjectHiberanteDAO;
import org.fage.semples.dao.ExamTestHiberanteDAO;
import org.fage.semples.dao.ExamTestTypeHiberanteDAO;
import org.fage.semples.model.ExamSubject;
import org.fage.semples.model.ExamSubjectVO;
import org.fage.semples.model.ExamTest;
import org.fage.semples.model.ExamTestType;
import org.fage.semples.model.ExamTestTypeVO;
import org.fage.semples.model.ExamTestVO;
import org.fage.semples.model.PagerModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class MessageTestServiceImpI implements MessageTestService {

	@Autowired
	private ExamTestHiberanteDAO examTestHiberanteDAO;
	
	@Autowired
	private ExamSubjectHiberanteDAO examSubjectHiberanteDAO;
	
	@Autowired
	private ExamTestTypeHiberanteDAO examTestTypeHiberanteDAO;
	
//	@Override
//	public List<ExamTest> findAllExamTestOrExamTestType() {
//		return examTestHiberanteDAO.findAllExamTestOrExamTestType();
//	}
	
	@Override
	public PagerModel<ExamTestVO> findAllByPage() {
		return examTestHiberanteDAO.findAllByPage();
	}
	
	@Override
	public List<ExamTestVO> findAllExamTests() {
		List<ExamTestVO> voList = new ArrayList<ExamTestVO>();
		List<ExamTest> examTestList = examTestHiberanteDAO.findAllExamTests();
		for(ExamTest examTest:examTestList){
			ExamTestVO examTestVO =new ExamTestVO();
			BeanUtils.copyProperties(examTest, examTestVO);
			examTestVO.setSubName(examTest.getExamSubject().getName());
			ExamTestTypeVO testTypeVO = new ExamTestTypeVO();
			BeanUtils.copyProperties(examTest.getExamTestType(), testTypeVO);
			examTestVO.setExamTestType(testTypeVO);
			voList.add(examTestVO);
		}
		return voList;
	}

	@Override
	public ExamTestVO getExamTestById(Integer id) {
		ExamTestVO examTestVO =new ExamTestVO();
		ExamTest examTest = examTestHiberanteDAO.getExamTestById(id);
		BeanUtils.copyProperties(examTest, examTestVO);
		examTestVO.setSubName(examTest.getExamSubject().getName());
		examTestVO.setSubId(Integer.parseInt(examTest.getExamSubject().getSubId()));
		ExamTestTypeVO testTypeVO = new ExamTestTypeVO();
		BeanUtils.copyProperties(examTest.getExamTestType(), testTypeVO);
		examTestVO.setExamTestType(testTypeVO);
		
		return examTestVO;
	}

	//添加
	@Override
	public void addTest(ExamTestVO examTestVO) {
		try {
			ExamTest examTest =new ExamTest();
			BeanUtils.copyProperties(examTestVO, examTest);
			ExamSubject examSubject = examSubjectHiberanteDAO.getExamSubjectById(examTestVO.getSubId().toString());
			examTest.setExamSubject(examSubject);
			ExamTestType examTestType = examTestTypeHiberanteDAO.getExamTestTypeById(examTestVO.getExamTestType().getId());
			examTest.setExamTestType(examTestType);
			examTestHiberanteDAO.createExamTest(examTest);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//科目
	@Override
	public List<ExamSubjectVO> getSubjectList() {
		List<ExamSubject> examSubjectList = examSubjectHiberanteDAO.findAllExamSubject();
		List<ExamSubjectVO> examSubjectVOList = new ArrayList<ExamSubjectVO>();
		for(ExamSubject examSubject:examSubjectList){
			ExamSubjectVO examSubjectVO = new ExamSubjectVO();
			BeanUtils.copyProperties(examSubject, examSubjectVO);
			examSubjectVOList.add(examSubjectVO);
		}
		return examSubjectVOList;
	}
	
	//题型
	@Override
	public List<ExamTestTypeVO> getTestTypeList() {
		List<ExamTestType> examTestTypeList = examTestTypeHiberanteDAO.findAllExamTestTypes();
		List<ExamTestTypeVO> examTestTypeVOList = new ArrayList<ExamTestTypeVO>();
		for(ExamTestType examTestType:examTestTypeList){
			ExamTestTypeVO examTestTypeVO = new ExamTestTypeVO();
			BeanUtils.copyProperties(examTestType, examTestTypeVO);
			examTestTypeVOList.add(examTestTypeVO);
		}
		return examTestTypeVOList;
	}
	
	@Override
	public void editExamTest(ExamTestVO examTestVO) throws Exception {
		ExamTest examTest = new ExamTest();
		BeanUtils.copyProperties(examTestVO, examTest);
		ExamSubject examSubject = examSubjectHiberanteDAO.getExamSubjectById(examTestVO.getSubId().toString());
		examTest.setExamSubject(examSubject);
		ExamTestType examTestType = examTestTypeHiberanteDAO.getExamTestTypeById(examTestVO.getExamTestType().getId());
		examTest.setExamTestType(examTestType);
		examTestHiberanteDAO.editExamTest(examTest);
	}

	@Override
	public void createExamTest(ExamTest examTest) throws Exception {
		examTestHiberanteDAO.createExamTest(examTest);
	}

	@Override
	public void deleteExamTest(Integer id) throws Exception {
		examTestHiberanteDAO.deleteExamTest(id);
	}

}
